package com.woniuxy.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.GoodsManagement;
import com.woniuxy.service.GoodsManagementService;
import com.woniuxy.utils.Result;

@Controller
@RequestMapping("/goods")
public class GoodsManagementController {

	@Autowired
	private GoodsManagementService goodsManagementService;

	// 查询所有产品
	@ResponseBody
	@RequestMapping("/all")
	public List<GoodsManagement> goodsManagements() {
		return goodsManagementService.findGoodsManagements();
	}

	// 添加产品
	@ResponseBody
	@RequestMapping("/addGoods")
	public Result<Object> addGoods(GoodsManagement goods) {
		Result<Object> result = new Result<>();
		goods.setTime(new Date());
		int addResult = goodsManagementService.addGoodsManagement(goods);

		if (addResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("数据插入成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("数据插入失败");
		}
		return result;
	}

	
	// 通过gmid查询当前行的数据
	@ResponseBody
	@RequestMapping("/searchData")
	public GoodsManagement searchData(int gmid) {
		return goodsManagementService.findGoodsByGmid(gmid);
	}
	
	
	// 修改当前数据
	@ResponseBody
	@RequestMapping("/modifyGoods")
	public Result<Object> searchData(GoodsManagement goods) {
		System.out.println(goods.toString());
		
		Result<Object> result = new Result<>();
		goods.setTime(new Date());
		int newResult = goodsManagementService.modifyGoodsByGmid(goods);
		if (newResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("数据修改成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("数据修改失败");
		}
		return result;
	}

}
